'''
Date: 2024-03-03 00:55:33
LastEditors: ifredom ifredomvip@gmail.com
LastEditTime: 2024-03-22 00:16:56
FilePath: \spider\main.py
scrap
'''
import asyncio
import argparse
import sys
from core import config
from core.interface.crawler_impl import AbstractCrawler
from core.platform.xhs.core import XhsCore
from tools import utils


class CrawlerFactory:
    CRAWLERS = {
        "xhs": XhsCore
    }

    @staticmethod
    def create_crawler(platform: str) -> AbstractCrawler:
        crawler_class = CrawlerFactory.CRAWLERS.get(platform)
        if not crawler_class:
            raise ValueError(
                "Invalid Media Platform Currently only supported qqTheme ...")
        return crawler_class()

async def main():
    print("start scrap...")
    utils.logger.info(f"[main] start scrap...")

    parser = argparse.ArgumentParser(description='小红书自动回复')
    parser.add_argument('--platform', type=str, default=config.PLATFORM,
                        help='平台(小红书)',
                        choices=["xhs"])
    parser.add_argument('--lt', '--login_type', type=str, default=config.LOGIN_TYPE,
                        help='登录类型(qrcode | phone | cookie)',
                        choices=["qrcode", "phone", "cookie"])
    parser.add_argument('--type', type=str, help='crawler type (creator)',
                        choices=["creator"], default=config.CRAWLER_TYPE)
    parser.add_argument('--start', type=int, help='crawler type (number of start page)',
                        default=config.START_PAGE)
    parser.add_argument('--keywords', type=str, help='crawler type (please input keywords)',
                        default=config.KEYWORDS)

    args = parser.parse_args()
    crawler = CrawlerFactory.create_crawler(platform=args.platform)
    crawler.init_config(
        platform=args.platform,
        login_type=args.lt,
        crawler_type=args.type,
        start_page=args.start,
        keyword=args.keywords
    )
    await crawler.start()
    # CrawlerFactory.start_GUI()
    # await CrawlerFactory.start_new_crawler()

if __name__ == '__main__':
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        sys.exit()
